package co.yixiang.yshop.module.order.service.storeaftersalesstatus;

import co.yixiang.yshop.module.order.dal.dataobject.storeaftersalesstatus.StoreAfterSalesStatusDO;
import co.yixiang.yshop.module.order.dal.mysql.storeaftersalesstatus.StoreAfterSalesStatusMapper;
import co.yixiang.yshop.module.order.service.storeorder.AsyncStoreOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 售后订单操作详情 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class StoreAfterSalesStatusServiceImpl implements StoreAfterSalesStatusService {


    @Autowired
    private AsyncStoreOrderService asyncStoreOrderService;
    @Resource
    private StoreAfterSalesStatusMapper storeAfterSalesStatusMapper;


    /**
     * 添加订单操作记录
     *
     * @param oid           售后id
     * @param changeType    操作状态
     * @param changeMessage 操作内容
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void create(Long uid, Long oid, Integer changeType, String changeMessage) {
        StoreAfterSalesStatusDO storeAfterSalesStatus = new StoreAfterSalesStatusDO();

        storeAfterSalesStatus.setStoreAfterSalesId(oid);
        storeAfterSalesStatus.setChangeType(changeType);
        storeAfterSalesStatus.setChangeMessage(changeMessage);
        storeAfterSalesStatus.setChangeTime(LocalDateTime.now());
        storeAfterSalesStatus.setOperator(Objects.requireNonNull(getLoginUserId()).toString());
        storeAfterSalesStatusMapper.insert(storeAfterSalesStatus);

        //异步统计
        asyncStoreOrderService.orderData(uid);
    }

}
